﻿namespace ProjetoModeloDDD.MVC.Controllers
{
    using global::AutoMapper;
    using ProjetoModeloDDD.Application.Interface;
    using ProjetoModeloDDD.Domain.Entities;
    using ProjetoModeloDDD.MVC.ViewsModels;
    using System.Collections.Generic;
    using System.Web.Mvc;

    /// <summary>
    /// Classe Controller de clientes.
    /// </summary>
    public class ClientesController : Controller
    {
        /// <summary>
        /// O atributo privado de clienteAppService.
        /// </summary>
        private readonly IClienteAppService _clienteApp;

        /// <summary>
        /// Inicia uma nova instância da classe <see cref="ClientesController"/>.
        /// </summary>
        /// <param name="clienteApp">
        /// The cliente app.
        /// </param>
        public ClientesController(IClienteAppService clienteApp)
        {
            this._clienteApp = clienteApp;
        }

        // GET: Clientes

        /// <summary>
        /// The index.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Index()
        {
            var clienteViewModel = Mapper.Map<IEnumerable<Cliente>, IEnumerable<ClienteViewModel>>(this._clienteApp.GetAll());
            return View(clienteViewModel);
        }

        /// <summary>
        /// The especiais.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Especiais()
        {
            var clienteViewModel = Mapper.Map<IEnumerable<Cliente>, IEnumerable<ClienteViewModel>>(this._clienteApp.ObterClientesEspeciais());
            return View(clienteViewModel);
        }

        // GET: Clientes/Details/5
        public ActionResult Details(int id)
        {
            var cliente = this._clienteApp.GetById(id);
            var clienteViewModel = Mapper.Map<Cliente, ClienteViewModel>(cliente);
            return this.View(clienteViewModel);
        }

        // GET: Clientes/Create

        public ActionResult Create()
        {
            return this.View();
        }

        // POST: Clientes/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ClienteViewModel cliente)
        {
            if (ModelState.IsValid)
            {
                var clienteDomain = Mapper.Map<ClienteViewModel, Cliente>(cliente);
                this._clienteApp.Add(clienteDomain);

                return RedirectToAction("Index");
            }

            return this.View(cliente);
        }

        // GET: Clientes/Edit/5
        public ActionResult Edit(int id)
        {
            var cliente = this._clienteApp.GetById(id);
            var clienteViewModel = Mapper.Map<Cliente, ClienteViewModel>(cliente);
            return this.View(clienteViewModel);
        }

        // POST: Clientes/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ClienteViewModel cliente)
        {
            if (ModelState.IsValid)
            {
                var clienteDomain = Mapper.Map<ClienteViewModel, Cliente>(cliente);
                this._clienteApp.Update(clienteDomain);

                return RedirectToAction("Index");
            }
            return this.View(cliente);
        }

        // GET: Clientes/Delete/5
        public ActionResult Delete(int id)
        {
            var cliente = this._clienteApp.GetById(id);
            var clienteViewModel = Mapper.Map<Cliente, ClienteViewModel>(cliente);
            return this.View(clienteViewModel);
        }

        // POST: Clientes/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var cliente = this._clienteApp.GetById(id);
            this._clienteApp.Remove(cliente);

            return RedirectToAction("Index");
        }
    }
}